package com.bigzero.workflow.modules.system.controller;

import com.bigzero.workflow.common.exception.WorkflowException;
import com.bigzero.workflow.common.model.R;
import com.bigzero.workflow.modules.system.dto.MenuDto;
import com.bigzero.workflow.modules.system.entity.MenuEntity;
import com.bigzero.workflow.modules.system.query.MenuQuery;
import com.bigzero.workflow.modules.system.service.MenuService;
import com.bigzero.workflow.modules.system.vo.MenuVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理前端控制器
 * @author fanxinxiong
 * @since 2025-02-11
 */
@RestController
@RequestMapping("/system/menu")
@ApiSupport(order = 2)
@Tag(name = "系统管理-菜单管理")
public class MenuController {

    @Resource
    private MenuService menuService;

    /**
     * 树形表格
     * @param query
     * @return List<MenuVo>
     */
    //@PreAuthorize("hasRole('ROLE_superAdministrator')")
    @Operation(summary = "树形表格", description = "树形表格")
    @PreAuthorize("hasAuthority('system:menu:list')")
    @GetMapping(value = "treeTable")
    public R<List<MenuVo>> treeTable(MenuQuery query){
        return R.ok(menuService.treeTable("0", query));
    }

    /**
     * 详情
     *
     * @param menuId 菜单id
     * @return 用户详情
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 2)
    @Operation(summary = "详情")
    @Parameters({
       @Parameter(name = "menuId", description = "菜单id", example = "gf45e0320d93832c90dd3ed323129dd3", required = true)
    })
    @GetMapping("info/{menuId}")
    public R<MenuVo> info(@PathVariable String menuId) {
        MenuVo vo = menuService.getVoById(menuId);
        return R.ok(vo);
    }

    /**
     * 根据主键id查询菜单vo
     * @param id
     * @return MenuVo
     */
    //@PreAuthorize("hasRole('ROLE_superAdministrator')")
    @Operation(summary = "根据主键id查询菜单", description = "根据主键id查询菜单")
    @Parameters({
        @Parameter(name = "id", description = "菜单ID", example = "1", required = true)
    })
    @GetMapping(value = "getVoById")
    public MenuVo getVoById(@RequestParam(value = "id", required = true) String id){
        return menuService.getVoById(id);
    }

    /**
     * 表单校验
     * @param menuDto
     */
    public void formValidate(MenuDto menuDto) {
        if(StringUtils.isBlank(menuDto.getName())) {
            throw new WorkflowException("失败:菜单名称不能为空", 400);
        }
        if(menuDto.getType() == null) {
            throw new WorkflowException("失败:菜单类型不能为空", 400);
        }
        if(menuDto.getLinkType() == null) {
            throw new WorkflowException("失败:链接类型不能为空", 400);
        }
        if(menuDto.getSort() == null) {
            throw new WorkflowException("失败:序号不能为空", 400);
        }
        menuDto.setParentId(StringUtils.isBlank(menuDto.getParentId()) ? "0" : menuDto.getParentId());
    }

    /**
     * 保存
     *
     * @param menuDto 数据
     */
    //@PreAuthorize("hasRole('ROLE_superAdministrator')")
    @ApiOperationSupport(author = "fanxinxiong", order = 3)
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('system:menu:insert') or hasAuthority('system:menu:insert')")
    @PostMapping("save")
    public R<String> save(@RequestBody MenuDto menuDto) {
        //表单校验
        formValidate(menuDto);
        menuService.insertOrUpdate(menuDto);
        return R.ok();
    }

    /**
     * 删除
     * @param id 主键
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 4)
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('system:menu:delete')")
    @DeleteMapping("delete")
    public R<String> delete(@RequestBody String id) {
        if(StringUtils.isBlank(id)) {
            throw new WorkflowException("失败:菜单id不能为空");
        }
        menuService.deleteById(id);
        return R.ok();
    }

    /**
     * 菜单下拉树
     * @return List<MenuVo>
     */
    //@PreAuthorize("hasRole('ROLE_superAdministrator')")
    @Operation(summary = "菜单下拉树", description = "菜单下拉树", tags = {"系统管理-菜单管理", "系统管理-角色管理"})
    @GetMapping(value = "dropdownTree")
    public List<MenuVo> dropdownTree(){
        return menuService.dropdownTree();
    }

    /**
     * 父级菜单列表
     */
    @ApiOperationSupport(author = "fanxinxiong", order = 5)
    @Operation(summary = "父级菜单列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功响应",
            content = @Content(
                    mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = MenuEntity.class))
            ))
    })
    @GetMapping("parentMenuList")
    public R<List<MenuEntity>> parentMenuList(){
        List<MenuEntity> list = menuService.getParentMenuList();
        return R.ok(list);
    }
}
